﻿# The entry should be named in the same way that the elective succession law is named to define it's voter's logic.
# Root scope - Elector, the character voting in the election.
# scope:candidate scope - Candidate, Character considered for the title.
# scope:title scope - Title, The title the nomination is for.
# scope:holder scope - Ruler, The ruler currently holding the title.
# scope:holder_candidate scope - Favored Candidate, The title owner's candidate.
# The voter will vote for the top score candidate calculated from this script.

feudal_elective = {
	elector_vote_strength = {
		base = 1
		#modifier = { ##### Old, placed in canidate score
		#	desc = tooltip_feudal_elective_co_king
		#	add = 100
		#	this = scope:holder
		#	any_in_global_list = {
		#		variable  = cokingship_list
		#		this = scope:title
		#	}
		#}
		modifier = { #Sicilian Parliamentary Support
			desc = tooltip_feudal_vote_strength_parliament
			add = 1
			is_landed = yes
			exists = capital_county
			capital_county.county_opinion > 25
			capital_county = { exists = var:variable_sicilian_parliament_county }
		}
		modifier = { #Imperial Dignity
			desc = tooltip_feudal_vote_strength_emperor_tier
			add = 3
			highest_held_title_tier = 5
		}
		modifier = { #Royal Dignity
			desc = tooltip_feudal_vote_strength_king_tier
			add = 2
			highest_held_title_tier = 4
		}
		modifier = { #Princely Dignity
			desc = tooltip_feudal_vote_strength_duke_tier
			add = 1
			highest_held_title_tier = 3
		}
		modifier = { #Powerful Vassal
			desc = tooltip_feudal_vote_strength_powerful_vassal
			add = {
				value = 1
				if = { #Additional point if the vassal is more powerful than the current ruler (Vassal King who meets this condition will have more voting power than the Emperor himself (1+1+1+2 = 5))
					limit = {
						max_military_strength > scope:holder.max_military_strength
					}
					add = 1
				}
			}
			NOT = { this = scope:holder }
			is_powerful_vassal = yes
			liege = scope:holder
		}
		modifier = { #Clerical Influence
			desc = tooltip_feudal_vote_strength_theocrat
			add = 1
			NOT = { this = scope:holder }
			OR = {
				government_has_flag = government_is_theocracy
				has_council_position = councillor_court_chaplain
			}
			faith = scope:holder.faith
			faith = { has_doctrine = doctrine_theocracy_lay_clergy }
			#TODO_CD:Possible trigger checking for investiture succession?
		}
	}
	
	electors = {
		add = holder
		add = {
			type = title_dejure_vassals
			limit = {
				OR = { #1 or 2 tiers of difference for Electors.
					scope:title = { tier = tier_county }
					AND = {
						scope:title = { tier = tier_duchy }
						highest_held_title_tier >= 1
					}
					AND = {
						scope:title = { tier = tier_kingdom }
						highest_held_title_tier >= 2
					}
					AND = {
						scope:title = { tier = tier_empire }
						highest_held_title_tier >= 3
					}
				}
				OR = { #Either de facto direct vassal, or independent ruler who is de jure vassal. (no sub-vassals)
					is_vassal_of = scope:holder
					is_independent_ruler = yes
				}
				any_held_title = { #Elector title must be a de jure vassal of the title being voted on; multi-Dukes (or similar) might have two duchies in multiple electoral domains, which they should be able to vote on. This is only valid for the highest-tier title (so a Duke-Elector who also owns a County which is an electoral title for another title does not get a vote from that county).
					tier = root.highest_held_title_tier
					exists = de_jure_liege
					target_is_de_jure_liege_or_above = scope:title
				}
				this.faith = scope:holder.faith
			}
		}
		add = {
			type = holder_direct_vassals
			limit = {
				OR = { #1 or 2 tiers of difference for Electors.
					scope:title = { tier = tier_county }
					AND = {
						scope:title = { tier = tier_duchy }
						highest_held_title_tier >= 1
					}
					AND = {
						scope:title = { tier = tier_kingdom }
						highest_held_title_tier >= 2
					}
					AND = {
						scope:title = { tier = tier_empire }
						highest_held_title_tier >= 3
					}
				}
				NOT = {
					AND = {
						scope:title = { tier = tier_duchy }
						primary_title.de_jure_liege.holder ?= scope:holder
					}
					AND = {
						scope:title = { tier = tier_kingdom }
						highest_held_title_tier = tier_duchy
						primary_title.de_jure_liege.holder ?= scope:holder
					}
					AND = {
						scope:title = { tier = tier_kingdom }
						highest_held_title_tier = tier_county
						primary_title.de_jure_liege.de_jure_liege.holder ?= scope:holder
					}
					AND = {
						scope:title = { tier = tier_empire }
						highest_held_title_tier = tier_duchy
						primary_title.de_jure_liege.de_jure_liege.holder ?= scope:holder
					}
					AND = {
						scope:title = { tier = tier_empire }
						highest_held_title_tier = tier_kingdom
						primary_title.de_jure_liege.holder ?= scope:holder
					}
				}
			}
		}
	}

	candidate_score = {
		base = 0
		######################	Elector self-voting pattern	##########################
		elector_self_voting_pattern_feudal_elective_modifier = yes

		##########################	Holder voting pattern	##########################
		holder_voting_pattern_feudal_elective_modifier = yes

		##########################	Elector voting patterns (circumstances)	##########################
		elector_voting_pattern_circumstances_feudal_elective_modifier = yes

		##########################	Elector voting patterns (prestige/piety)	##########################
		elector_voting_pattern_prestige_piety_feudal_elective_modifier = yes

		##########################	Elector voting patterns (traits)	##########################
		elector_voting_pattern_traits_feudal_elective_modifier = yes

		##########################	Elector voting patterns (opinion)	##########################
		elector_voting_pattern_opinion_feudal_elective_modifier = yes
		##########################  Makes co-king work ##########################################
		elector_voting_pattern_opinion_co_king = yes
	}
	
	#scope:candidate = Candidate, scope:title = Title, scope:holder_candidate = Candidate currently selected by ruler.
	candidates = {
		add = {
			type = holder_direct_vassals
			limit = {
				feudal_elective_potential_landed_candidate_trigger = yes
			}
		}
		add = {
			type = title_dejure_vassals
			limit = {
				is_independent_ruler = yes #De jure vassals of the title who are not de facto vassals of the current ruler.
				feudal_elective_potential_landed_candidate_trigger = yes
			}
		}
		add = {
			type = title_claimants
			limit = {
				feudal_elective_potential_landless_claimant_candidate_trigger = yes
			}
		}
		add = {
			type = holder_close_or_extended_family
			limit = {
				is_close_family_of = scope:holder
				feudal_elective_potential_landless_dynastic_candidate_trigger = yes
			}
		}
	}
}
